package com.icim.webservice;
/**
 * 服务端(监听MCS->MES消息，并返回消息至MCS01.REPLY)
 */

import com.rabbitmq.client.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

@Controller("consumerController")
public class ConsumerController {
	private Logger logger = Logger.getLogger(ConsumerController.class);
	private static final String QUEUE_NAME = "SILVER_PARAM";
	private static final String IP_ADDRESS_LOCAL = "10.0.3.65";
	private static final int PORT = 5672;

	private static final String IP_ADDRESS_REMOTE = "10.0.3.65";

	private static final String QUEUE_NAME_TEST="ZF_TETST";
	private static final String EXCHANGE_NAME = "exchange_demo";
	private static final String ROUTING_KEY = "ZF_TETST";


	/**
	 * 初始化创建MQ连接
	 * @throws IOException
	 */
	@PostConstruct
	public void initRpcConsumer() throws IOException,TimeoutException,InterruptedException{
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost(IP_ADDRESS_LOCAL);
		factory.setPort(PORT);
		factory.setUsername("guest");
		factory.setPassword("guest");
		Connection connection = factory.newConnection();//创建连接
		Channel channel = connection.createChannel();//创建信道

          /* 声明要连接的队列 */
		channel.queueDeclare(QUEUE_NAME, true, false, false, null);
		System.out.println("等待消息产生：");

        /* 创建消费者对象，用于读取消息 */
		QueueingConsumer consumer = new QueueingConsumer(channel);
		channel.basicConsume(QUEUE_NAME, true, consumer);

        /* 读取队列，并且阻塞，即在读到消息之前在这里阻塞，直到等到消息，完成消息的阅读后，继续阻塞循环 */
		while (true) {
			QueueingConsumer.Delivery delivery = consumer.nextDelivery();
			String message = new String(delivery.getBody(),"UTF-8");
			testPorvider(message);
			System.out.println("收到消息'" + message + "'");
		}
    }

	public void testPorvider(String msg)throws IOException,TimeoutException,InterruptedException{
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost(IP_ADDRESS_REMOTE);
		factory.setPort(PORT);
		factory.setUsername("guest");
		factory.setPassword("guest");
		Connection connection = factory.newConnection();//创建连接
		Channel channel = connection.createChannel();//创建信道
		//创建一个type="direct"、持久化的、非自动删除的交换器
		channel.exchangeDeclare(EXCHANGE_NAME, "direct",true,false,null);
		//创建一个持久化、非排他的、非自动删除的队列
		channel.queueDeclare(QUEUE_NAME_TEST,true,false,false,null);
		//将交换器与队列通过路由键绑定
		channel.queueBind(QUEUE_NAME_TEST,EXCHANGE_NAME,ROUTING_KEY);
		//发送一条持久化的消息：hello  world!
		String message = msg;
		channel.basicPublish(EXCHANGE_NAME,ROUTING_KEY,
				MessageProperties.PERSISTENT_TEXT_PLAIN,
				message.getBytes());
		//关闭资源
		channel.close();
		connection.close();
	}


//	public void testConsumber2()throws IOException,TimeoutException,InterruptedException{
//		ConnectionFactory factory = new ConnectionFactory();
//		factory.setHost(IP_ADDRESS_LOCAL);
//		factory.setPort(PORT);
//		factory.setUsername("guest");
//		factory.setPassword("guest");
//		Connection connection = factory.newConnection();//创建连接
//		Channel channel = connection.createChannel();//创建信道
//
//          /* 声明要连接的队列 */
//		channel.queueDeclare(QUEUE_NAME, true, false, false, null);
//		System.out.println("等待消息产生：");
//
//        /* 创建消费者对象，用于读取消息 */
//		QueueingConsumer consumer = new QueueingConsumer(channel);
//		channel.basicConsume(QUEUE_NAME, true, consumer);
//
//        /* 读取队列，并且阻塞，即在读到消息之前在这里阻塞，直到等到消息，完成消息的阅读后，继续阻塞循环 */
//		while (true) {
//			QueueingConsumer.Delivery delivery = consumer.nextDelivery();
//			String message = new String(delivery.getBody());
//			testPorvider(message);
//			System.out.println("收到消息'" + message + "'");
//		}
//	}


}
